package com.yeshen.pmtea.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.yeshen.pmtea.R;

import java.util.Timer;
import java.util.TimerTask;

public class FirstActivity extends AppCompatActivity {

    private TextView mTextView;
    private int num =3;
    private boolean tag=false;
    private SharedPreferences sp;
    private Timer mTimer;
    private Handler mHandler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 99:
                      mTextView.setText(num+"hha");
                    break;
            }

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        getSupportActionBar().hide();
       // Log.d("flag", "---------------onCreate: " +"log测试");

        mTextView = (TextView) findViewById(R.id.tv);
        sp = getSharedPreferences("appInfo", MODE_PRIVATE);
        tag = sp.getBoolean("aha", false);
        //定时器来做
         mTimer=new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(99);
                num--;
                if (num==1){
                    toNextActivity();
                }
            }
        },1000,1000);

    }

    private void toNextActivity() {
        Intent intent=new Intent();
        if (tag==false){
            intent.setClass(this,TwoActivity.class);
            SharedPreferences.Editor edit = sp.edit();
            edit.putBoolean("aha",true);
            edit.commit();
        }else {
            intent.setClass(this,MainActivity.class);
        }
        startActivity(intent);
        finish();
    }
}

